/******************************************************************************
 * @File name   :      ReportServiceImpl.java
 *
 * @Author      :      LUZHANG
 *
 * @Date        :      Sep 1, 2015
 *
 * @Copyright Notice: 
 * Copyright (c) 2015 Capgemini, Inc. All  Rights Reserved.
 * This software is published under the terms of the Capgemini Software
 * License version 1.0, a copy of which has been included with this
 * distribution in the LICENSE.txt file.
 * 
 * 
 * ----------------------------------------------------------------------------
 * Date                   Who         Version        Comments
 * Sep 1, 2015 5:57:12 PM        LUZHANG     1.0            Initial Version
 *****************************************************************************/
package com.cap.bts.crmdp.dealer.report.service.impl;


import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.cap.bts.crmdp.dealer.logistic.dao.IMaintainLogisticDao;
import com.cap.bts.crmdp.dealer.logistic.dto.LogisticRelatedDto;
import com.cap.bts.crmdp.dealer.mars.service.IMarsPackageDefinitionService;
import com.cap.bts.crmdp.dealer.report.dao.IReportDao;
import com.cap.bts.crmdp.dealer.report.dto.InventoryDataDto;
import com.cap.bts.crmdp.dealer.report.dto.InventoryResultDto;
import com.cap.bts.crmdp.dealer.report.dto.MemberChartDto;
import com.cap.bts.crmdp.dealer.report.dto.MemberChartResultDto;
import com.cap.bts.crmdp.dealer.report.dto.MemberReportResultDto;
import com.cap.bts.crmdp.dealer.report.dto.ReportQueryDto;
import com.cap.bts.crmdp.dealer.report.dto.SalesReportDetailsDto;
import com.cap.bts.crmdp.dealer.report.dto.SalesReportDto;
import com.cap.bts.crmdp.dealer.report.dto.TranHistoryDto;
import com.cap.bts.crmdp.dealer.report.dto.SalesReportResultDto;
import com.cap.bts.crmdp.dealer.report.service.IReportService;
import com.cap.bts.framework.common.exception.AppException;
import com.cap.bts.framework.common.service.impl.BaseServiceImpl;

/**
 *
 */
@Transactional(rollbackFor = AppException.class)
@Service
public class ReportServiceImpl extends BaseServiceImpl  implements IReportService  {

    @Autowired
    IMarsPackageDefinitionService packageDefinitionService;
    
    @Resource(name = "maintainLogisticDao")
    private IMaintainLogisticDao maintainLogisticDao;

    @Resource(name = "reportDao")
    private IReportDao reportDao;
    
    @Override
    public InventoryResultDto queryInventoryData(ReportQueryDto dto){
        InventoryResultDto result = new InventoryResultDto();
        if(dto != null) {
            //List<CardDefinition> dataList = new ArrayList<CardDefinition>()
            List<LogisticRelatedDto> dataList = maintainLogisticDao.getCardName();
            /*try {
                List<PackageDefinitionResultsDto> data=packageDefinitionService.getAllPackageDefinitionList(Long.valueOf(dto.getUserId()))
                for(int i=0;i<data.size();i++){
                    if(data.get(i).getCardDefinition()!=null && !dataList.contains(data.get(i).getCardDefinition())){
                        dataList.add(data.get(i).getCardDefinition())
                    }
                }
            } catch (AppException e) {
                LOGGER.error(e)
            }*/
            InventoryDataDto summary = new InventoryDataDto();
            /*summary.setOrderAmount("0")
            summary.setSellAmount("0")
            summary.setMonthlyAvg("0")*/
            summary.setInventoryAmount("0");
            summary.setNotReceivedAmount("0");
            summary.setSuggestAmount("0");
            /*List<InventoryDataDto> resultList = maintainLogisticDao.queryInventoryDataOnRoad(dto, 
                maintainLogisticDao.queryInventoryDataInventory(dto,
                    maintainLogisticDao.queryInventoryAllSelled(dto,
                        maintainLogisticDao.queryInventoryDataSelled(dto,
                            maintainLogisticDao.getMonthAmt(dto,
                                maintainLogisticDao.queryInventoryDataOrdered(dto,dataList))))))*/
            List<InventoryDataDto> resultList = maintainLogisticDao.queryInventoryDataOnRoad(dto, 
                maintainLogisticDao.queryInventoryDataInventory(dto,
                    maintainLogisticDao.queryInventoryAllSelled(dto,
                        maintainLogisticDao.getMonthAmt(dto,
                            maintainLogisticDao.queryInventoryDataOrdered(dto,dataList)))));
            for(int i=0;i<resultList.size();i++){
                /*summary.setOrderAmount(String.valueOf(Integer.parseInt(summary.getOrderAmount())+Integer.parseInt(resultList.get(i).getOrderAmount())))
                summary.setSellAmount(String.valueOf(Integer.parseInt(summary.getSellAmount())+Integer.parseInt(resultList.get(i).getSellAmount())))
                summary.setMonthlyAvg(String.valueOf(Integer.parseInt(summary.getMonthlyAvg())+Integer.parseInt(resultList.get(i).getMonthlyAvg())))*/
                summary.setInventoryAmount(String.valueOf(Integer.parseInt(summary.getInventoryAmount())+Integer.parseInt(resultList.get(i).getInventoryAmount())));
                summary.setNotReceivedAmount(String.valueOf(Integer.parseInt(summary.getNotReceivedAmount())+Integer.parseInt(resultList.get(i).getNotReceivedAmount())));
                summary.setSuggestAmount(String.valueOf(Integer.parseInt(summary.getSuggestAmount())+Integer.parseInt(resultList.get(i).getSuggestAmount())));
            }
            result.setResultList(resultList);
            result.setResultSummary(summary);
            result.setSuccess(true);
        }
        return result;
    }

    @Override
    public String queryInventoryDataForChart(ReportQueryDto dto) throws AppException{
        String result = "";
        InventoryResultDto resultForChart = new InventoryResultDto();
        if(dto != null) {
            List<LogisticRelatedDto> dataList = maintainLogisticDao.getSelledCard(dto);
            try {
                result=maintainLogisticDao.queryInventoryDataSelledForChart(dto,dataList
                    ,maintainLogisticDao.getAllMonth(dto));
            } catch (AppException e) {
                throw new AppException("inventoryChart", e);
            }
            resultForChart.setSuccess(true);
        }
        return result;
    }
    
    @Override
    public MemberReportResultDto queryMemberData(ReportQueryDto dto){
        MemberReportResultDto result = new MemberReportResultDto();
        if(dto.getUserId()!=null 
            && dto.getTransactionStartDate()!=null 
            && dto.getTransactionEndDate()!=null
            && dto.getPackageId()!=null ){
            /*result.setResult(maintainLogisticDao.getHistoryPackageQty(
                maintainLogisticDao.getHistoryQty(
                    maintainLogisticDao.getNewCar(
                        maintainLogisticDao.getNewPackage(
                            maintainLogisticDao.getNewMember(dto))))));*/
            result.setResult(maintainLogisticDao.getHistoryPackageQty(
                maintainLogisticDao.getHistoryQty(
                    maintainLogisticDao.getNewMember(dto))));
            result.setSuccess(true);
        }
        return result;
    }
    
    @Override
    public MemberReportResultDto queryMemberDetails(ReportQueryDto dto){
        MemberReportResultDto result = new MemberReportResultDto();
        if(dto.getUserId()!=null 
            && dto.getTransactionStartDate()!=null 
            && dto.getTransactionEndDate()!=null
            && dto.getPackageId()!=null ){
            result.setResultListMmbr(maintainLogisticDao.getNewMemberDetails(dto));
            result.setResultListPkg(maintainLogisticDao.getNewPackageDetails(dto));
            result.setResultListCr(maintainLogisticDao.getNewCarDetails(dto));
            result.setSuccess(true);
        }
        return result;
    }
    
    @Override
    public MemberChartResultDto queryMemberDataForChart(ReportQueryDto dto) throws AppException{
        MemberChartResultDto result = new MemberChartResultDto();
        if(dto!=null){
            List<String> months=maintainLogisticDao.getAllMonth(dto);
            List<MemberChartDto> dataList = new ArrayList<MemberChartDto>();
            List<Integer> maxList = new ArrayList<Integer>();
            for(String month:months){
                MemberChartDto data = maintainLogisticDao.queryMemberDataForChart(dto,month);
                maxList.add(Integer.parseInt(data.getNewMember()));
                data = maintainLogisticDao.queryPackageDataForChart(data,dto.getHierarchy(),dto.getPackageId());
                maxList.add(Integer.parseInt(data.getNewPackage()));
                data = maintainLogisticDao.queryCarDataForChart(data,dto.getHierarchy());
                maxList.add(Integer.parseInt(data.getNewCar()));
                dataList.add(data);
            }
            result.setMaxList(maxList);
            result.setMemberChartData(dataList);
            result.setSuccess(true);
        }
        return result;
    }

    @Override
    public  List<TranHistoryDto> queryTranHistoryListByMemberId(Long memberId, Long dealerId,String carName)
        throws AppException {
        return reportDao.getTranHistoryListByMemberId(memberId, dealerId,carName);
    }
    
    @Override
    public SalesReportResultDto querySalesVolume(ReportQueryDto dto){
        SalesReportResultDto result = new SalesReportResultDto();
        if(dto.getUserId()!=null 
            && dto.getTransactionStartDate()!=null 
            && dto.getTransactionEndDate()!=null
            && dto.getPackageId()!=null){
            if(dto.getUserId().equals(dto.getHierarchy())){
                //result.setResult(maintainLogisticDao.getSalesVolume(dto,dto.getUserId()))
                result.setResult(maintainLogisticDao.getSalesReport(dto,dto.getUserId()));
            }else{
                String[] ary = dto.getHierarchy().split("','");
                SalesReportDto dataResult = new SalesReportDto();
                dataResult.setPrePay(0);
                dataResult.setUsedPrePay(0);
                dataResult.setNotUsedPrePay(0);
                dataResult.setExpiredPrePay(0);
                dataResult.setIncomePrePay(0);
                dataResult.setIncomeNotPrePay(0);
                /*dataResult.setSalesPrePay(0)
                dataResult.setUsedValue(0)
                dataResult.setSalvageValue(0)
                dataResult.setActualSalesVolume(0)
                dataResult.setAllNotUsedValue(0)*/
                for(String dealerNo: ary){
                    //SalesReportDto data=maintainLogisticDao.getSalesVolume(dto,dealerNo)
                    SalesReportDto data=maintainLogisticDao.getSalesReport(dto,dealerNo);
                    dataResult.setPrePay(dataResult.getPrePay()+data.getPrePay());
                    dataResult.setUsedPrePay(dataResult.getUsedPrePay()+data.getUsedPrePay());
                    dataResult.setNotUsedPrePay(dataResult.getNotUsedPrePay()+data.getNotUsedPrePay());
                    dataResult.setExpiredPrePay(dataResult.getExpiredPrePay()+data.getExpiredPrePay());
                    dataResult.setIncomePrePay(dataResult.getIncomePrePay()+data.getIncomePrePay());
                    dataResult.setIncomeNotPrePay(dataResult.getIncomeNotPrePay()+data.getIncomeNotPrePay());
                    /*dataResult.setSalesPrePay(dataResult.getSalesPrePay()+data.getSalesPrePay())
                    dataResult.setUsedValue(dataResult.getUsedValue()+data.getUsedValue())
                    dataResult.setSalvageValue(dataResult.getSalvageValue()+data.getSalvageValue())
                    dataResult.setActualSalesVolume(dataResult.getActualSalesVolume()+data.getActualSalesVolume())
                    dataResult.setAllNotUsedValue(dataResult.getAllNotUsedValue()+data.getAllNotUsedValue())*/
                }
                result.setResult(dataResult);
            }
            //result.setPrePayDetails(maintainLogisticDao.getPeriodPrePayDetails(dto))
            //result.setUsedDetails(maintainLogisticDao.getPeriodUsedDetails(dto))
            //result.setExpireDetails(maintainLogisticDao.getPeriodExpiredDetails(dto))
            result.setSuccess(true);
        }
        return result;
    }

    @Override
    public SalesReportResultDto exportSalesReport(ReportQueryDto salesDto){
        SalesReportResultDto result = new SalesReportResultDto();
        if(salesDto.getHierarchy()!=null && salesDto.getType()!=null && salesDto.getPackageId()!=null){
            result.setDetails(maintainLogisticDao.exportSalesReport(salesDto));
            result.setSuccess(true);
        }
        return result;
    }
    
    @Override
    public SalesReportResultDto querySalesVolumeDetails(ReportQueryDto dto){
        SalesReportResultDto result = new SalesReportResultDto();
        if(dto.getUserId()!=null 
            && dto.getPackageId()!=null ){
            result.setNotUsedDetails(maintainLogisticDao.getAccumulateNotUsedDetails(dto));
            result.setSuccess(true);
        }
        return result;
        }
}
